package com.jtny.nytb.config.measurementunit.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jtny.component.entity.PageData;
import com.jtny.component.entity.WebResponse;
import com.jtny.component.exception.ServiceException;
import com.jtny.component.util.BeanUtils;
import com.jtny.nytb.config.excel.biz.ImportExcelBiz;
import com.jtny.nytb.config.excel.util.EasyExcelUtils;
import com.jtny.nytb.config.measurementunit.dto.MeasurementUnitDTO;
import com.jtny.nytb.config.measurementunit.service.MeasurementUnitService;
import com.jtny.nytb.config.measurementunit.vo.ExcelMeasurementUnitVO;
import com.jtny.nytb.config.measurementunit.vo.MeasurementUnitPageQueryParamVO;
import com.jtny.nytb.config.measurementunit.vo.MeasurementUnitVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 计量单位Controller类
 * @package com.jtny.nytb.config.measurementUnit.controller
 * @fileName MeasurementUnitController.java
 * @author 智慧能源中心-lyq
 * @createTime 2023年09月01日 11:15
 * @version 1.0
 * @Copyright© 2023 jtny
 * 江投能源技术研究院 版权所有
 */
@RestController
@Api(value = "计量单位管理", tags = "计量单位管理")
@RequestMapping("/config/measurementUnit")
@AllArgsConstructor
public class MeasurementUnitController {
    @Resource
    private MeasurementUnitService measurementUnitService;
    @Resource
    private ImportExcelBiz importExcelBiz;

    private static final String OPER_SUCCESS_MESSAGE = "操作成功！";

    @ApiOperation("计量单位新增操作")
    @ApiImplicitParam(name = "measurementUnitVO", value = "计量单位新增操作", required = true, paramType = "query", dataType = "MeasurementUnitVO")
    @ApiOperationSupport(order = 1)
    @PostMapping("/save")
    public WebResponse<?> saveMeasurementUnit(@RequestBody @Valid MeasurementUnitVO measurementUnitVO) {
        try{
            MeasurementUnitVO measurementUnit = measurementUnitService.saveMeasurementUnit(measurementUnitVO);
            return WebResponse.success(OPER_SUCCESS_MESSAGE, measurementUnit);
        }catch (ServiceException e){
            return WebResponse.error(e);
        }
    }

    @ApiOperation("计量单位更新操作")
    @ApiImplicitParam(name = "measurementUnitVO", value = "计量单位更新操作", required = true, paramType = "query", dataType = "MeasurementUnitVO")
    @ApiOperationSupport(order = 2)
    @PostMapping("/update")
    public WebResponse<?> updateMeasurementUnit(@RequestBody @Valid MeasurementUnitVO measurementUnitVO) {
        try{
            measurementUnitService.updateMeasurementUnit(measurementUnitVO);
            return WebResponse.success(OPER_SUCCESS_MESSAGE);
        }catch (ServiceException e){
            return WebResponse.error(e);
        }

    }

    @ApiOperation("计量单位查看操作")
    @ApiImplicitParam(name = "measurementUnitId", value = "计量单位分类id", required = true, paramType = "path")
    @ApiOperationSupport(order = 3)
    @GetMapping("/detail/{measurementUnitId}")
    @ResponseBody
    public WebResponse<?> detailMeasurementUnit(@PathVariable("measurementUnitId") Long measurementUnitId) {
        MeasurementUnitVO measurementUnitVO = measurementUnitService.detailMeasurementUnit(measurementUnitId);
        return WebResponse.success(measurementUnitVO);
    }

    @ApiOperation("计量单位分页查询操作")
    @ApiImplicitParam(name = "measurementUnitPageQueryParamVO", value = "计量单位分页查询操作", required = true, paramType = "query", dataType = "MeasurementUnitPageQueryParamVO")
    @ApiOperationSupport(order = 4)
    @PostMapping("/queryPage")
    public WebResponse<?> pageQuery(@RequestBody @Valid MeasurementUnitPageQueryParamVO measurementUnitPageQueryParamVO) {
        PageData<MeasurementUnitVO> measurementUnitVOPageData = measurementUnitService.queryByPage(measurementUnitPageQueryParamVO);
        return WebResponse.success(measurementUnitVOPageData);
    }

    /**
     * 删除计量单位
     * @param measurementUnitId 计量单位Id
     * @throws ServiceException 抛出异常
     */
    @ApiOperation("删除计量单位")
    @ApiImplicitParam(name = "measurementUnitId", value = "计量单位id", required = true, paramType = "path")
    @ApiOperationSupport(order = 5)
    @GetMapping("/delete/{measurementUnitId}")
    @ResponseBody
    public WebResponse<?> deleteMeasurementUnit(@PathVariable("measurementUnitId") Long measurementUnitId){
        measurementUnitService.deleteMeasurementUnit(measurementUnitId);
        return WebResponse.success(OPER_SUCCESS_MESSAGE);
    }

    /**
     * 启用计量单位
     * @param measurementUnitId 计量单位Id
     * @throws ServiceException 抛出异常
     */
    @ApiOperation("启用计量单位")
    @ApiImplicitParam(name = "measurementUnitId", value = "计量单位id", required = true, paramType = "path")
    @ApiOperationSupport(order = 6)
    @GetMapping("/enable/{measurementUnitId}")
    @ResponseBody
    public WebResponse<?> enableMeasurementUnit(@PathVariable("measurementUnitId") Long measurementUnitId){
        measurementUnitService.enableMeasurementUnit(measurementUnitId);
        return WebResponse.success(OPER_SUCCESS_MESSAGE);
    }

    /**
     * 禁用计量单位
     * @param measurementUnitId 计量单位Id
     * @throws ServiceException 抛出异常
     */
    @ApiOperation("禁用计量单位")
    @ApiImplicitParam(name = "measurementUnitId", value = "计量单位id", required = true, paramType = "path")
    @ApiOperationSupport(order = 7)
    @GetMapping("/disable/{measurementUnitId}")
    @ResponseBody
    public WebResponse<?> disableMeasurementUnit(@PathVariable("measurementUnitId") Long measurementUnitId){
        measurementUnitService.disableMeasurementUnit(measurementUnitId);
        return WebResponse.success(OPER_SUCCESS_MESSAGE);
    }

    /**
     * 获取导入模板
     * @param response 响应
     */
    @PostMapping("/export-template")
    @ApiOperation(value = "获取导入模板")
    public void exportTemplate(HttpServletResponse response) {
        EasyExcelUtils.exportExcel(response, "计量单位分类导入模版", null, ExcelMeasurementUnitVO.class);
    }

    @ApiOperation("导入计量单位分类")
    @ApiImplicitParam(name = "file", value = "计量单位分类excel文件", required = true, paramType = "query", dataType = "MultipartFile")
    @ApiOperationSupport(order = 9)
    @PostMapping("/import")
    public WebResponse<?> importMeasurementUnit(@RequestParam(value = "file") MultipartFile file) {
        List<ExcelMeasurementUnitVO> list = EasyExcelUtils.importExcel(file, ExcelMeasurementUnitVO.class);
        importExcelBiz.importMeasurementUnitExcel(list);
        return WebResponse.success();
    }

    @ApiOperation("导出计量单位分类")
    @ApiOperationSupport(order = 10)
    @PostMapping("/export")
    @ApiImplicitParam(name = "idList", value = "id列表", required = true, paramType = "body", dataType = "List<Long>")
    @ResponseBody
    public void exportMeasurementUnit(HttpServletResponse response, @RequestBody List<Long> idList) {
        MeasurementUnitPageQueryParamVO paramVO = new MeasurementUnitPageQueryParamVO();
        paramVO.setIdList(idList);
        paramVO.setPageSize(10000);
        PageData<MeasurementUnitVO> page = measurementUnitService.queryByPage(paramVO);
        List<ExcelMeasurementUnitVO> toList = BeanUtils.copyToList(page.getData(), ExcelMeasurementUnitVO.class);
        EasyExcelUtils.exportExcel(response, "数据字典", toList, ExcelMeasurementUnitVO.class);
    }
}
